package cst.a214frm.startproject;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import cst.a214frm.startproject.bean.WeatherInfo;
import cst.a214frm.startproject.listener.WeatherListener;
import cst.a214frm.startproject.model.Weather51Model;
import cst.a214frm.startproject.model.WeatherModel;

public class MainActivity extends AppCompatActivity implements View.OnClickListener , WeatherListener {
    private EditText cityNOInput;
    private TextView city;
    private TextView temp;
    private TextView wd;
    private TextView ws;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        cityNOInput = (EditText) findViewById(R.id.cityno);
        city = (TextView) findViewById(R.id.city);
        temp = (TextView) findViewById(R.id.temp);
        wd = (TextView) findViewById(R.id.WD);
        ws = (TextView) findViewById(R.id.WS);

        findViewById(R.id.button1).setOnClickListener(this);
        findViewById(R.id.button2).setOnClickListener(this);

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.button1:

                WeatherModel weatherModel = new WeatherModel();
                weatherModel.getWeather(cityNOInput.getText().toString(), this);

                break;
            case R.id.button2:


                Weather51Model weather51Model = new Weather51Model();
                weather51Model.getWeather(cityNOInput.getText().toString(), this);

                break;
            default:
                break;
        }
    }

    @Override
    public void onResponse(WeatherInfo weather) {
        if (weather != null) {
            city.setText(weather.getCity());
            temp.setText(weather.getTemp());
            wd.setText(weather.getWD());
            ws.setText(weather.getWS());
        } else {
            city.setText("未知");
        }
    }

    @Override
    public void onFail(String msg) {
        city.setText(msg);
    }
}
